<!--
 * @Author: HeAo
 * @Date: 2021-10-15 14:05:01
 * @LastEditTime: 2021-10-15 14:37:49
 * @LastEditors: HeAo
 * @Description: 
 * @FilePath: \web2103-2105\web2103code\3-2-Vue\day07\1-原生ajax.html
 * 别乱动！
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../js/qs.js"></script>
  <script>
    const qs = Qs
    const baseUrl = 'http://47.94.46.113:8888'
    const queryData = {
      page: 1,
      pageSize: 10
    }
    const loginData = {
      username: 'admin1',
      password: 123321
    }
    const applyData = {
      name: 'tom',
      age: 12,
      telephone: 13455667788,
      gender: 'male'
    }

    // 1.实例化http请求对象
    let xhr = new XMLHttpRequest()

    // 2.打开连接，设置请求的配置参数
    // xhr.open('get', baseUrl + '/index/category/findAll')
    // const qsData = qs.stringify(queryData)
    // xhr.open('get', baseUrl + '/index/article/pageQuery' + '?' + qsData)
    // xhr.open('post', baseUrl + '/user/login')
    // xhr.setRequestHeader(
    //   'Content-Type',
    //   'application/json'
    // )
    xhr.open('post', baseUrl + '/user/login')
    xhr.setRequestHeader(
      'Content-Type',
      'application/x-www-form-urlencoded'
    )

    // 3.发送请求并且配置请求体中的参数
    // xhr.send(JSON.stringify(loginData))
    xhr.send(qs.stringify(applyData))

    // 4.监听http连接状态的改变，获取响应
    xhr.onreadystatechange = () => {
      if (xhr.readyState == 4 && xhr.status == 200) {
        console.log(JSON.parse(xhr.response));
      }
    }
  </script>
</head>

<body>

</body>

</html>